package com.wondertek.web.controller.sys;

import cn.hutool.captcha.LineCaptcha;
import com.wondertek.common.base.Result;

import com.wondertek.common.constant.LanguageCodeStatus;
import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.SecurityUtil;

import com.wondertek.service.dto.app.res.AppAuthCodeRes;
import com.wondertek.service.dto.sys.req.SysLoginReq;
import com.wondertek.service.dto.sys.req.SysLoginSmsReq;
import com.wondertek.service.dto.sys.req.SysLoginWxReq;
import com.wondertek.service.dto.sys.req.SysUserForgetReq;
import com.wondertek.service.dto.sys.res.SysLoginRes;
import com.wondertek.service.entity.sys.SysCountryInfoPo;
import com.wondertek.service.service.sys.ISysAuthService;
import com.wondertek.service.service.sys.ISysCountryInfoService;
import com.wondertek.service.service.sys.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/auth/")
@Tag(name = "系统登录管理")
public class SysAuthController {

    @Resource
    private ISysAuthService sysAuthService;

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysCountryInfoService sysCountryInfoService;

    @GetMapping("/getCountryInfo")
    @Operation(summary = "获取国家信息")
    public Result<List<SysCountryInfoPo>> getCountryInfo(){
        return Result.success(sysCountryInfoService.list());
    }

    /*
     * 邮箱获取验证码
     * */
    @GetMapping("/email/code")
    @Operation(summary = "邮箱获取验证码")
    public Result<?> emailCode(@RequestParam(name="email") String email){
        //校验邮箱是否注册
        long emailNum = userService.checkEmail(email);
        if(emailNum == 0){
            return Result.error("email.not.register");
        }
        AppAuthCodeRes appAuthCodeRes = new AppAuthCodeRes();
        appAuthCodeRes.setCode("1234");
        //通过邮箱获取验证码，保存的有效时间
        return Result.success(appAuthCodeRes);
    }

    /*
    * 校验邮箱验证码
    * */
    @GetMapping("/check/code")
    @Operation(summary = "校验邮箱验证码")
    public Result<?> checkCode(@RequestParam(name="code") String code){
        //从redis获取验证码
        if("1234".equals(code)){
            return Result.success("validate.success");
        }else {
            return Result.error("captcha.error");
        }
    }

    /**
     * 获取验证码
     * @param formId /
     */
    @GetMapping("/captcha")
    @Operation(summary = "获取验证码")
    public void captcha(@RequestParam(name="formId") String formId,
                     HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = sysAuthService.createCaptcha(formId);
        lineCaptcha.write(response.getOutputStream());
    }

    /*
    * 忘记密码-修改密码
    * */
    @PostMapping("/forget/restPassword")
    @Operation(summary = "忘记密码-修改密码")
    public Result<?> forgetAndRestPassword(@RequestBody @Validated SysUserForgetReq passwordReq){
        userService.forgetAndRestPassword(passwordReq);
        return Result.success("update.success");
    }

    /**
     * 用户登录
     * @param sysLoginReq /
     * @return /
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    @AutoLog(category = "sys", handleType = "log.login",
            handleNote = "'登录系统:'+#sysLoginReq.email+'账户'")
    public Result<SysLoginRes> login(@Validated @RequestBody SysLoginReq sysLoginReq){
        return Result.success(sysAuthService.login(sysLoginReq));
    }


    /**
     * 【短信登录】-获取验证码
     * @param mobile /
     */
    @GetMapping("/sendSmsCode")
    @Operation(summary = "【短信登录】-获取验证码")
    public Result<?> sendSmsCode(@RequestParam(name="mobile") String mobile) throws Exception {
        sysAuthService.sendSmsCode(mobile);
        return Result.success("sms.send.success");
    }

    /**
     * 【短信登录】-用户登录
     * @param sysLoginSmsReq /
     * @return /
     */
    @PostMapping("/smsLogin")
    @Operation(summary = "【短信登录】-用户登录")
    @AutoLog(category = "sys", handleType = "log.login",
            handleNote = "'短信登录系统:'+#sysLoginReq.username+'账户'")
    public Result<SysLoginRes> smsLogin(@Validated @RequestBody SysLoginSmsReq sysLoginSmsReq){
        return Result.success(sysAuthService.smsLogin(sysLoginSmsReq));
    }

    /**
     * 【微信登录】-用户登录
     * @param sysLoginWxReq /
     * @return /
     */
    @PostMapping("/wxLogin")
    @Operation(summary = "【微信登录】-用户登录(关联openid)")
    public Result<SysLoginRes> wxLogin(@Validated @RequestBody SysLoginWxReq sysLoginWxReq){
        String username = SecurityUtil.getLoginUser().getUsername();
        sysLoginWxReq.setUsername(username);
        return Result.success(sysAuthService.wxLogin(sysLoginWxReq));
    }

    /**
     * 获取当前登录用户信息
     * @return /
     */
    @GetMapping("/info")
    @Operation(summary = "获取当前登录用户信息")
    public Result<SysLoginRes> loginInfo(){
        String token = SecurityUtil.getLoginUser().getToken();
        return Result.success(sysAuthService.loginInfo(token));
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @Operation(summary = "退出登录")
    @AutoLog(category = "sys", handleType = "log.logout",
            handleNote = "'登出系统:'+@securityUtil.getLoginUser().getUsername()+'账户'")
    public Result<?> logout() {
        String token = SecurityUtil.getLoginUser().getToken();
        sysAuthService.logout(token);
        return Result.success("user.logout.success");
    }

}
